package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.MstCodeMapper;
import com.lst.entity.request.PlanReqPara;
import com.lst.entity.response.SurveyResPara;
import com.lst.model.MstCode;
import com.lst.model.Plan;
import com.lst.service.PlanService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: PlanOutServlet
 * @Description: 退出计划接口
 * 
 */
public class PlanSurveyServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private PlanService planService = ctx.getBean(PlanService.class);
	
	@Autowired
	private MstCodeMapper mstCodeMapper = ctx.getBean(MstCodeMapper.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);
		SurveyResPara resPara = new SurveyResPara();

		try {
			PlanReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			    Integer userid = Integer.valueOf(reqPara.getUserid());
			    
				 Map<String,Object> rusultMap=new HashMap<String,Object>();
	    		 rusultMap.put("createUser", userid);
	    		 rusultMap.put("orderByClause", "md.createDate desc");
	    		 List<Plan> planList=planService.queryByList(rusultMap);
	    		 Plan plan=planList.get(0);
	    		 
	    		 int identityType=plan.getIdentityType();
	    		 int trainType=plan.getTrainType();
	    		 int kfClassType=plan.getKfClassType();
	    		 
	    		MstCode mstCode= mstCodeMapper.selectByPrimaryKey(identityType);
	    		resPara.setIdentityName(mstCode.getName());
	    		
	    		MstCode mstCode2= mstCodeMapper.selectByPrimaryKey(trainType);
	    		resPara.setTrainName(mstCode2.getName());
	    		
	    		MstCode mstCode3= mstCodeMapper.selectByPrimaryKey(kfClassType);
	    		resPara.setKfName(mstCode3.getName());
	    		
	    		resPara.setIdentityType(identityType);
	    		resPara.setTrainType(trainType);
	    		resPara.setKfClassType(kfClassType);
				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);
			
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info("PlanSurveyServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public PlanReqPara getReqPara(HttpServletRequest request) {

		PlanReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new PlanReqPara());

			reqPara = (PlanReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
		
			
			logger.info("reqPara userid:" + userid);
		
	        reqPara.setUserid(userid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		PlanReqPara cdReqPara = (PlanReqPara) reqPara;
		SurveyResPara cdResPara = (SurveyResPara) resPara;

	  if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else {
			flag = true;
		}

		return  flag;
	}

}
